#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct stu
{
    char name[10];
    int score;
}STU;

typedef struct node
{
    STU data;
    struct node *pnext;
} Node, *Link;

/*遍历链表并统计结点个数*/
int displayNode(Link head);

/*查找指定位置上的链表数据*/
int findnode(Link head, int pos);

/*查找链表上的数据输出所在的位置*/
int findnode1(Link head, char name[10]);

/*头插法创建链表*/
void createList(Link head, char name[10],int score);

/*尾插法创建链表*/
void createList1(Link head, char name[10],int score);

/*创建头结点*/
Link createheadNode()
{
    Link pnew = (Link)malloc(sizeof(Node));
    pnew->pnext = NULL;
    return pnew;
}

int main()
{
    /*创建头结点*/
    Link headnode = createheadNode();

    createList(headnode,"luo",57);
    createList(headnode,"xie",88);
    createList(headnode,"chen",37);

    //findnode1(headnode,"luo");
    displayNode(headnode);
}

int displayNode(Link head)
{
    int count = 0;
    Link p = head->pnext;
    while (p != NULL)
    {
        printf("%s %d\n",p->data.name,p->data.score);
        p = p->pnext;
        count++;
    }

    return count;
}

int findnode(Link head, int pos)
{
    int count = 0;
    Link p = head->pnext;
    while (p != NULL)
    {
        if (count == pos)
        {
            printf(" %d\n", p->data.score);
            return p->data.score;
        }
        count++;
        p = p->pnext;
    }
}

int findnode1(Link head, char name[10])
{
    while (head != NULL)
    {
        if (strcmp(head ->data.name,name) == 0)
        {
            printf("这位学生的成绩是：%d\n",head ->data.score);
            return head ->data.score;
        }

        head = head->pnext;
    }
    return -1;
}

void createList(Link head, char name[10],int score)
{
    Link pnew = (Link)malloc(sizeof(Node));
    
    strcpy(pnew->data.name,name);
    pnew->data.score = score;

    pnew->pnext = head->pnext;
    head->pnext = pnew;
}

void createList1(Link head, char name[10],int score)
{
    Link pnew = (Link)malloc(sizeof(Node));
    strcpy(pnew->data.name,name);
    pnew->data.score = score;

    while (head ->pnext != NULL)
        head = head->pnext;

    head -> pnext = pnew;
    pnew->pnext = NULL;
}
